package com.hiwhitley.himei.module.game;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.contract.GameContract;
import com.hiwhitley.himei.contract.model.GameModel;
import com.hiwhitley.himei.contract.presenter.GamePresenter;
import com.hiwhitley.himei.entity.GameDesc;
import com.hiwhitley.himei.entity.GameHeader;
import com.hiwhitley.himei.entity.GameItem;
import com.hiwhitley.himei.entity.GamePoster;
import com.hiwhitley.himei.mvpframe.base.BaseFrameFragment;
import com.hiwhitley.himei.widget.PinnedHeaderDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by hiwhitley on 2016/10/5.
 */

public class GameListFragment extends BaseFrameFragment<GamePresenter, GameModel> implements GameContract.View {
    private static final String TAG = "GameListFragment";
    @BindView(R.id.game_recyclerview)
    RecyclerView mGameRecyclerview;
    @BindView(R.id.root_view)
    LinearLayout mRootView;

    private List<GameItem> mGameItems;
    private MultiTypeAdapter mMultiTypeAdapter;
    private int offset = 0;
    private int limit = 20;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_game_list;
    }

    @Override
    protected void init() {
        mPresenter.loadGames(offset, limit);
    }

    @Override
    protected void setListener() {

        mGameItems = new ArrayList<>();

        mMultiTypeAdapter = new MultiTypeAdapter();

        mMultiTypeAdapter.register(GameHeader.class, new GameHeaderViewProvider());
        mMultiTypeAdapter.register(GamePoster.class, new GamePostersViewProvider());
        mMultiTypeAdapter.register(GameDesc.class, new GameDescViewProvider());

        mGameRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        mGameRecyclerview.setAdapter(mMultiTypeAdapter);

        PinnedHeaderDecoration pinnedHeaderItemDecoration = new PinnedHeaderDecoration();
        pinnedHeaderItemDecoration.setPinnedTypeHeader(GameItem.TYPE_HEADER);
        pinnedHeaderItemDecoration.registerTypePinnedHeader(GameItem.TYPE_HEADER);
        pinnedHeaderItemDecoration.registerTypePinnedHeader(GameItem.TYPE_DESC);

        mGameRecyclerview.addItemDecoration(pinnedHeaderItemDecoration);

    }

    @Override
    public void showGames(List<GameItem> gameItems) {
        mMultiTypeAdapter.setItems(gameItems);
        mMultiTypeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestEnd() {

    }
}
